package com.fuershiye.woman.monitor.utils;

import com.fuershiye.woman.monitor.beans.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Pattern;

public class BaseUtil {

	/** int */
	private final static Pattern isInt = Pattern.compile("^-?[1-9]\\d*$");
	private final static Pattern isDouble = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$");


	public static String myReplace(String str) {
		return str.toUpperCase().replace("A","T");
	}

	public static String myTrim(String str) {
		return str.trim();
	}


	public static Boolean modela(int aim) {

		return ((aim-2)%3)==0;
	}

	public static int suba(int aim) {
		return (aim-2)/3;
	}

	//采用正则表达式的方式来判断一个字符串是否为数字，这种方式判断面比较全
	//可以判断正负、整数小数
	public static Boolean isNumber(String str) {

		return isInt.matcher(str).find()
				|| isDouble.matcher(str).find();

	}

	/**
	 * @param ori 用英文逗号隔开的字符串
	 * @return
	 */
	public static List<Integer> getListIntFromStr(String ori) {

		List<Integer> result = Lists.newArrayList();

		if(StringUtils.isBlank(ori)){
			return result;
		}

		String [] yearArray = ori.trim().split(",");
		for (String str : yearArray) {
			if(isNumber(str)){
				result.add(Integer.parseInt(str));
			}
		}
		return result;
	}

	public static List<Long> getIds(List<IdName> list) {
		List<Long> result = Lists.newArrayList();

		if(list.isEmpty()){
			return result;
		}

		for (IdName idName:list){
			if(idName==null){
				continue;
			}
			result.add(idName.getId());
		}
		return result;
	}

	public static boolean departBelongArea(long departid, long areaid, List<IdAreaId> departmentList) {
		for (IdAreaId idAreaId:departmentList){
			if(idAreaId!=null&&
					idAreaId.getId().equals(departid)&&
					idAreaId.getAreaid().equals(areaid)){
				return true;
			}
		}
		return false;
	}

	public static Map<Long, String> changeIdNamePidToMap(List<IdNamePid> list) {

		Map<Long, String> map = Maps.newHashMap();
		
		if(list==null||list.isEmpty()){
			return map;
		}
		for (IdNamePid idNamePid : list) {
			map.put(idNamePid.getId(), idNamePid.getName());
		}
		
		return map;
	}

	public static String getNameById(List<IdName> list, long id) {

		if(list==null||list.isEmpty()){
			return "";
		}
		for (IdName idName : list) {
			if(id==idName.getId()){
				return idName.getName();
			}
		}
		return "";
	}

	public static List<Long> removeDuplicateList(List<Long> list) {
		Set<Long> setWithoutDuplicates = new HashSet<>(list);
		List<Long> result = new ArrayList<>(setWithoutDuplicates);
		return result;
	}

	public static List<IdName> removeDuplicateIdName(List<IdName> list) {

		List<IdName> result = Lists.newArrayList();
		if(list==null||list.isEmpty()){
			return result;
		}

		Map<Long, String> map = Maps.newHashMap();
		for (IdName idName : list) {
			if(null==idName){continue;}
			if(map.containsKey(idName.getId())){continue;}
			map.put(idName.getId(), idName.getName());
		}

		for (Map.Entry<Long, String> entry: map.entrySet()){
			IdName idName = new IdName();
			idName.setId(entry.getKey());
			idName.setName(entry.getValue());
			result.add(idName);
		}

		return result;
	}


	public static void main(String[] args) throws Exception {

//		System.out.println("a".replace("A","T").toUpperCase());

		System.out.println(myTrim("22    ").equals("22"));
		System.out.println(myTrim("22  ").equals("22"));
		System.out.println(myTrim(" 22").equals("22"));
		System.out.println(myTrim("   22").equals("22"));

//		System.out.println(" 2 ".equals("2"));
//		System.out.println(" 2".equals("2"));
//		System.out.println("2 ".equals("2"));
//
//		System.out.println(myTrim(" 2 ").equals("2"));
//		System.out.println(myTrim(" 2").equals("2"));
//		System.out.println(myTrim("2 2").equals("22"));

//		System.out.println(isNumber(1+"s"));
//		System.out.println(isNumber("-.01"));
//		System.out.println(isNumber(0.1+""));
//		System.out.println(isNumber(-0.1+""));
//		System.out.println(isNumber(1.1+""));
//		System.out.println(isNumber(-1.1+""));
//		System.out.println(isNumber(1+""));
//		System.out.println(isNumber(-11+""));
//		System.out.println(isNumber(0+""));
//		System.out.println(isNumber("10000000000000000000000"));
//		System.out.println(isNumber("10000000000"));
//		System.out.println(isNumber("0.0000000001"));
//		System.out.println(isNumber("-0.0000000001"));

//		System.out.println(suba(5));
//		System.out.println(suba(8));
//		System.out.println(suba(11));

//		System.out.println(modela(2));
//		System.out.println(modela(4));
//		System.out.println(modela(5));
//		System.out.println(modela(6));
//		System.out.println(modela(7));
//		System.out.println(modela(8));
//		System.out.println(modela(11));

//		System.out.println("here!");
//		List<IdName> list = Lists.newArrayList();
//
//		IdName idName1 = new IdName();
//		idName1.setId(1L);
//		idName1.setName("1");
//		list.add(idName1);
//
//		IdName idName2 = new IdName();
//		idName2.setId(2L);
//		idName2.setName("2");
//		list.add(idName2);
//
//		IdName idName3 = new IdName();
//		idName3.setId(3l);
//		idName3.setName("3");
//		list.add(idName3);
//
//		System.out.println(getNameById(list, 1));

	}

}